package com.hualan.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.mapper.PsychologyTestTitleMapper;
import com.hualan.sfzy.mapper.PsychologyTitleOptionMapper;
import com.hualan.sfzy.model.entity.Answer;
import com.hualan.sfzy.model.entity.PsychologyTestTitle;
import com.hualan.sfzy.model.entity.PsychologyTitleOption;
import com.hualan.sfzy.service.BaseService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PsychologyTestTitleServiceImpl implements BaseService<PsychologyTestTitle> {
    @Autowired
    private PsychologyTestTitleMapper Mapper;
    @Autowired
    private PsychologyTitleOptionMapper optionMapper;

    /*添加*/
    @Override
    public int add(PsychologyTestTitle entity) {
        return Mapper.insert(entity);
    }

    /*修改*/
    @Override
    public int edit(PsychologyTestTitle entity) {
        return Mapper.update(entity);
    }

    /*删除*/
    @Override
    public int remove(Integer id) {
        return Mapper.delete(id);
    }

    /*批量删除*/
    @Override
    public int removeBatch(Integer[] ids) {
        return Mapper.deleteBatch(ids);
    }

    /*查询所有*/
    @Override
    public List<PsychologyTestTitle> find() {
        return Mapper.select(null);
    }

    /*分页查询*/
    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, PsychologyTestTitle entity) {
        PageHelper.startPage(pageNum, pageSize);
        List<PsychologyTestTitle> list = Mapper.select(entity);
        PageInfo<PsychologyTestTitle> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>();
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }

    /*根据id查询*/
    @Override
    public PsychologyTestTitle findById(Integer id) {
        return Mapper.selectById(id);
    }

    /*根据id查询(多表)*/
    public PsychologyTestTitle findByIdDuo(Integer id) {
        return Mapper.selectByIdDuo(id);
    }

    /*查询所有(多表)*/
    //public Map<String, Object> findByCondition(Integer pageNum, Integer pageSize, PsychologyTestTitle entity) {
    //    Integer count = Mapper.selectCountDuo(entity);
    //    //计算分页数据
    //    Integer offset = (pageNum - 1) * pageSize;
    //    Integer pages=(int)Math.ceil((double)count/pageSize);
    //
    //    Integer page=pageSize*4;
    //
    //    List<PsychologyTestTitle> list = Mapper.selectAllDuo(entity,offset,page);
    //    Map<String, Object> map = new HashMap<>();
    //    map.put(CommonConstant.PAGE_LIST, list);
    //    map.put(CommonConstant.PAGE_TOTAL, pages);
    //    map.put(CommonConstant.TOTAL, count);
    //    return map;
    //}

    /*查询所有(多表)*/
    public Map<String, Object> findByCondition(Integer pageNum, Integer pageSize, PsychologyTestTitle entity) {
        PageHelper.startPage(pageNum, pageSize * 4);
        List<PsychologyTestTitle> list = Mapper.selectAllDuo(entity);
        PageInfo<PsychologyTestTitle> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>();
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal() % 4 == 0 ? pageInfo.getTotal() / 4 : pageInfo.getTotal() / 4 + 1);
        return map;
    }

    /*根据title查询*/
    public List<Map<String, Object>> selectAllTitle() {
        return Mapper.selectAllTitle();
    }

    /*多表添加业务*/
    /*前端获取来数据，获取实体的title和options，把题目添加到PsychologyTestTitle实体对象并返回id，并把id传给PsychologyTitleOption实体一并将选项和id循环添加到选项表*/
    // ... existing code ...

    @Transactional
    public int addDuo(PsychologyTestTitle entity) {
        // 精确查询是否已存在相同标题的题目
        PsychologyTestTitle query = new PsychologyTestTitle();
        query.setTitle(entity.getTitle());
        query.setTestId(entity.getTestId());
        List<PsychologyTestTitle> titles = Mapper.selectAllDuo(query);
        if (!titles.isEmpty()) {
            throw new RuntimeException("题目'" + entity.getTitle() + "'已存在");
        }
        //添加题目
        Mapper.insert(entity);
        //获取添加后的id
        Integer id = entity.getId();
        //获取选项
        List<PsychologyTitleOption> options = entity.getOptions();
        //循环添加选项
        for (PsychologyTitleOption option : options) {
            option.setTestTitleId(id);
            optionMapper.insertSelective(option);
        }
        return 1;
    }
    /*多表编辑业务*/
    @Transactional
    public int editDuo(PsychologyTestTitle entity) {
        //检查标题是否已存在
        PsychologyTestTitle query = new PsychologyTestTitle();
        query.setTitle(entity.getTitle());
        query.setTestId(entity.getTestId());
        List<PsychologyTestTitle> titles = Mapper.selectAllDuo(query);
        if (!titles.isEmpty() && !titles.get(0).getId().equals(entity.getId())) {
            throw new RuntimeException("题目'" + entity.getTitle() + "'已存在");
        }
        // 更新题目信息
        Mapper.update(entity);
        //获取题目ID
        Integer id = entity.getId();
        // 查询现有选项
        List<PsychologyTitleOption> oldOptions = optionMapper.selectByTestTitleId(id);
        // 删除旧选项
        for (PsychologyTitleOption oldOption : oldOptions) {
            optionMapper.deleteByPrimaryKey(oldOption.getId());
        }
        //添加新选项
        List<PsychologyTitleOption> newOptions = entity.getOptions();
        if (newOptions != null) {
            for (PsychologyTitleOption option : newOptions) {
                option.setTestTitleId(id);
                optionMapper.insertSelective(option);
            }
        }
        return 1;
    }
    /*根据testid查找对应的题目和选项*/
    public List<PsychologyTestTitle> selectByTestId(Integer testId) {
        List<PsychologyTestTitle> list = Mapper.selectByTestId(testId);
        for (PsychologyTestTitle map : list) {
            Integer id = map.getId();
            List<PsychologyTitleOption> options = optionMapper.selectByTestTitleId(id);
            map.setOptions(options);
        }
        return list;
    }
}
